/**
 * 
 */
package com.maszy.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.maszy.bean.Print;
import com.maszy.db.DBConnection;

/** 
 * @作者 Allen Shu 
 * @创建日期 2012-6-6 
 * @版本 V 1.0 
 */

public class PrintService {
	private static PreparedStatement pst;
	private static ResultSet rs = null;
	public int add(Print obj) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="INSERT INTO [Maszy_Print]([print_name], [print_ip], [print_remark]) VALUES(?,?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {			
			pst.setString(1,obj.getPrint_name());
			pst.setString(2,obj.getPrint_ip());
			pst.setString(3,obj.getRemark());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}
	public int update(Print obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="UPDATE [Maszy_Print] SET [print_name]=?, [print_ip]=?, [print_remark]=? WHERE print_id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);			
			pst.setString(1, obj.getPrint_name());
			pst.setString(2, obj.getPrint_ip());
			pst.setString(3, obj.getRemark());
			pst.setInt(4, obj.getPrint_id());
			i=pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public int delete(Print obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="delete from  Maszy_Print  where print_id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getPrint_id());
			i=pst.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public Print queryById(Print obj){
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_Print where print_id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setInt(1, obj.getPrint_id());
			
			rs = pst.executeQuery();
			while (rs.next()) {
				obj=new Print();
				obj.setPrint_id(rs.getInt("print_id"));
				obj.setPrint_name(rs.getString("print_name"));
				obj.setPrint_ip(rs.getString("print_ip"));
				obj.setRemark(rs.getString("print_remark"));
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return obj;
	}

	public List<Print> queryAll(){
		List<Print> list=new ArrayList<Print>();
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_Print";
		try {
			pst = con.getConnection().prepareStatement(sql);				
			rs = pst.executeQuery();
	
			while (rs.next()) {
				Print obj=new Print();
				obj.setPrint_id(rs.getInt("print_id"));
				obj.setPrint_name(rs.getString("print_name"));
				obj.setPrint_ip(rs.getString("print_ip"));
				obj.setRemark(rs.getString("print_remark"));
				list.add(obj);
			}
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return list;
	}
}
